﻿using Coolzon.Common;
using Coolzon.UI.Common.Event.Web;
using Coolzon.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Coolzon.UI.Common.WebProxies
{
    public class WebEventProxy
    {
        public static WebEventProxy Instance = new WebEventProxy();

        /// <summary>
        /// 这个需要提前设置好
        /// </summary>
        public string BaseUrl { get; set; }

        [Subscribe]
        public async void OnReceiveGetSolutionAlbumListBegin(GetSolutionAlbumListBegin @event)
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                try
                {
                    var response = await client.GetAsync("api/solution/albumlist");
                    var result = await response.Content.ReadAsAsync<ServiceResult<List<SolutionAlbum>>>();
                    response.EnsureSuccessStatusCode(); // Throw on error code（有错误码时抛出）.
                    if (result.IsSuccess)
                    {
                        GetSolutionAlbumListEnd endEvent = new GetSolutionAlbumListEnd(WebEventState.Finished);
                        endEvent.SolutionAlbumList = result.Data;
                        WebEventBus.PublishAsync(endEvent);
                    }
                    else
                    {
                        GetSolutionAlbumListEnd endEvent = new GetSolutionAlbumListEnd(WebEventState.Failure);
                        WebEventBus.PublishAsync(endEvent);
                    }
                }
                catch (Exception ex)
                {
                    GetSolutionAlbumListEnd endEvent = new GetSolutionAlbumListEnd(WebEventState.Failure);
                    endEvent.Error = ex;
                    WebEventBus.PublishAsync(endEvent);
                }
            }
        }

        #region login event
        [Subscribe]
        public async void OnReceiveLoginBegin(LoginBegin @event)
        {
            LoginParam param = new LoginParam();
            param.AppType = @event.AppType;
            param.AppVersion = @event.AppVersion;
            param.DotNetVersion = @event.DotNetVersion;
            param.OSVersion = @event.OSVersion;
            param.SolutionSelectionMode = @event.SolutionSelectionMode;
            param.StartupPath = @event.StartupPath;
            param.UserAccount = @event.UserAccount;
            param.UserPassword = @event.UserPassword;

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUrl);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                try
                {
                    var response = await client.PostAsJsonAsync<LoginParam>("api/account/login", param);
                    var result = await response.Content.ReadAsAsync<ServiceResult<LoginResult>>();
                    response.EnsureSuccessStatusCode(); // Throw on error code（有错误码时抛出）.
                    if (result.IsSuccess)
                    {
                        LoginEnd endEvent = new LoginEnd(WebEventState.Finished);
                        endEvent.UserIdentity = result.Data.UserIdentity;
                        WebEventBus.PublishAsync(endEvent);
                    }
                    else
                    {
                        LoginEnd endEvent = new LoginEnd(WebEventState.Failure);
                        WebEventBus.PublishAsync(endEvent);
                    }
                }
                catch (Exception ex)
                {
                    LoginEnd endEvent = new LoginEnd(WebEventState.Failure);
                    endEvent.Error = ex;
                    WebEventBus.PublishAsync(endEvent);
                }
            }
        }
        #endregion
    }
}
